#include"Stack.h"

void StackInit(Stack* ps)
{
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
void Capacitytest(Stack* ps)
{
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDateType* temp = (STDateType*)realloc(ps->arr, sizeof(STDateType) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
}
void StackPush(Stack* ps, STDateType x)
{
	Capacitytest(ps);
	ps->arr[ps->top++] = x;
}

void StackPrint(Stack* ps)
{
	for (int i = 0; i < ps->top; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

void StackPop(Stack* ps)
{
	assert(!StackEmpty(ps));
	ps->top--;
}

STDateType StackTop(Stack* ps)
{
	assert(!StackEmpty(ps));
	STDateType x = ps->arr[ps->top - 1];
	return x;
}

int StackSize(Stack* ps)
{
	return ps->top;
}

int StackEmpty(Stack* ps)
{
	if (ps->top == 0)
	{
		return 1;
	}
	return 0;
}

void StackDestroy(Stack* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}